package org.anddev.andengine.extension.tbsengine.message.server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.UUID;

import org.anddev.andengine.extension.tbsengine.message.ICopyable;
import org.anddev.andengine.extension.tbsengine.player.BasePlayer;

/**
 * This message should be used to announce that a specific player joined or 
 * left a match preparation or a running match.
 * 
 * @author Tobias Boehm
 *
 */
public class ServerMatchPlayerJoinedLeftAnnouncementMessage extends AbsServerMatchMessage implements DefaultTBSserverMessageFlags, ICopyable<ServerMatchPlayerJoinedLeftAnnouncementMessage> {

	public static enum PlayerAnnouncementType{JOINED,LEFT,BANNED};
	
	private UUID mPlayerID = null;
	private String mPlayerName = "";
	private PlayerAnnouncementType mActionType = PlayerAnnouncementType.LEFT;
	
	public ServerMatchPlayerJoinedLeftAnnouncementMessage(){
		
	}
	
	public ServerMatchPlayerJoinedLeftAnnouncementMessage(final BasePlayer pPlayer, final PlayerAnnouncementType pActionType){
		
		this.mPlayerID = pPlayer.getPlayerID();
		
		this.mPlayerName = pPlayer.getPlayerName();
		
		this.mActionType = pActionType;
	}
	
	@Override
	public short getFlag() {
		
		return FLAG_SERVER_MATCH_PLAYER_JOINED_LEFT_ANNOUNCEMENT;
	}
	
	@Override
	protected void onReadTransmissonMatchData(DataInputStream pDataInputStream)
			throws IOException {
		
		this.mPlayerID = UUID.fromString(pDataInputStream.readUTF());
		
		this.mPlayerName = pDataInputStream.readUTF();
		
		this.mActionType = PlayerAnnouncementType.valueOf(pDataInputStream.readUTF());
	}

	@Override
	protected void onWriteTransmissonMatchData(
			DataOutputStream pDataOutputStream) throws IOException {

		pDataOutputStream.writeUTF(this.mPlayerID.toString());
		
		pDataOutputStream.writeUTF(this.mPlayerName);
		
		pDataOutputStream.writeUTF(this.mActionType.name());
	}

	/**
	 * @return the mPlayerID
	 */
	public UUID getPlayerID() {
		return mPlayerID;
	}


	/**
	 * @return the mPlayerName
	 */
	public String getPlayerName() {
		return mPlayerName;
	}

	public void setPlayer(final BasePlayer pPlayer){
		
		this.mPlayerID = pPlayer.getPlayerID();
		
		this.mPlayerName = pPlayer.getName();
	}
	
	
	/**
	 * @return the mActionType
	 */
	public PlayerAnnouncementType getAnnouncementType() {
		return mActionType;
	}

	/**
	 * @param mActionType the mActionType to set
	 */
	public void setActionType(PlayerAnnouncementType mActionType) {
		this.mActionType = mActionType;
	}

	@Override
	public ServerMatchPlayerJoinedLeftAnnouncementMessage copy() {
		
		final ServerMatchPlayerJoinedLeftAnnouncementMessage copy = new ServerMatchPlayerJoinedLeftAnnouncementMessage();
		
		copy.mActionType = this.mActionType;
		
		copy.mMatchID = this.mMatchID;
		
		copy.mPlayerID = this.mPlayerID;
		
		copy.mPlayerName = this.mPlayerName;
		
		return copy;		
	}



}
